knitr::opts_chunk$set(cache = TRUE, autodep = TRUE)
library(rethinking)
## Loading required package: rstan
## Warning: package 'rstan' was built under R version 3.2.5
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.2.4
## Loading required package: StanHeaders
## Warning: package 'StanHeaders' was built under R version 3.2.5
## rstan (Version 2.12.1, packaged: 2016-09-11 13:07:50 UTC, GitRev: 85f7a56811da)
## For execution on a local, multicore CPU with excess RAM we recommend calling
## rstan_options(auto_write = TRUE)
## options(mc.cores = parallel::detectCores())
## Loading required package: parallel
## rethinking (Version 1.59)
library(brms)
## Warning: package 'brms' was built under R version 3.2.5
## Loading 'brms' package (version 1.1.0). Useful instructions
## can be found by typing help('brms'). A more detailed introduction
## to the package is available through vignette('brms').
##
## Attaching package: 'brms'
## The following objects are masked from 'package:rethinking':
##
## LOO, stancode, WAIC
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
The prior of normal(0,1) will provide more shrinkage
Instead of
a_group ~ Normal(0,10)
use
a_group ~ normal(a,sigma) a ~ (0,10) sigma ~ cacuhy(0,1)
alpha only
data(reedfrogs, results='hide')
## Warning in data(reedfrogs, results = "hide"): data set 'hide' not found
d <- reedfrogs
d$tank <- 1:nrow(d)
m12m1.tank <- map2stan(
alist(
surv ~ dbinom( density , p ) ,
logit(p) <- a_tank[tank] ,
a_tank[tank] ~ dnorm( a , sigma ) ,
a ~ dnorm(0,1) ,
sigma ~ dcauchy(0,1)
), data=d , iter=4000 , chains=4 )
## Warning in FUN(X[[i]], ...): data with name pred is not numeric and not
## used
## Warning in FUN(X[[i]], ...): data with name size is not numeric and not
## used
##
## SAMPLING FOR MODEL 'surv ~ dbinom(density, p)' NOW (CHAIN 1).
##
## Chain 1, Iteration: 1 / 4000 [ 0%] (Warmup)
## Chain 1, Iteration: 400 / 4000 [ 10%] (Warmup)
## Chain 1, Iteration: 800 / 4000 [ 20%] (Warmup)
## Chain 1, Iteration: 1200 / 4000 [ 30%] (Warmup)
## Chain 1, Iteration: 1600 / 4000 [ 40%] (Warmup)
## Chain 1, Iteration: 2000 / 4000 [ 50%] (Warmup)
## Chain 1, Iteration: 2001 / 4000 [ 50%] (Sampling)
## Chain 1, Iteration: 2400 / 4000 [ 60%] (Sampling)
## Chain 1, Iteration: 2800 / 4000 [ 70%] (Sampling)
## Chain 1, Iteration: 3200 / 4000 [ 80%] (Sampling)
## Chain 1, Iteration: 3600 / 4000 [ 90%] (Sampling)
## Chain 1, Iteration: 4000 / 4000 [100%] (Sampling)
## Elapsed Time: 0.328575 seconds (Warm-up)
## 0.282204 seconds (Sampling)
## 0.610779 seconds (Total)
##
##
## SAMPLING FOR MODEL 'surv ~ dbinom(density, p)' NOW (CHAIN 2).
##
## Chain 2, Iteration: 1 / 4000 [ 0%] (Warmup)
## Chain 2, Iteration: 400 / 4000 [ 10%] (Warmup)
## Chain 2, Iteration: 800 / 4000 [ 20%] (Warmup)
## Chain 2, Iteration: 1200 / 4000 [ 30%] (Warmup)
## Chain 2, Iteration: 1600 / 4000 [ 40%] (Warmup)
## Chain 2, Iteration: 2000 / 4000 [ 50%] (Warmup)
## Chain 2, Iteration: 2001 / 4000 [ 50%] (Sampling)
## Chain 2, Iteration: 2400 / 4000 [ 60%] (Sampling)
## Chain 2, Iteration: 2800 / 4000 [ 70%] (Sampling)
## Chain 2, Iteration: 3200 / 4000 [ 80%] (Sampling)
## Chain 2, Iteration: 3600 / 4000 [ 90%] (Sampling)
## Chain 2, Iteration: 4000 / 4000 [100%] (Sampling)
## Elapsed Time: 0.333265 seconds (Warm-up)
## 0.27771 seconds (Sampling)
## 0.610975 seconds (Total)
##
##
## SAMPLING FOR MODEL 'surv ~ dbinom(density, p)' NOW (CHAIN 3).
##
## Chain 3, Iteration: 1 / 4000 [ 0%] (Warmup)
## Chain 3, Iteration: 400 / 4000 [ 10%] (Warmup)
## Chain 3, Iteration: 800 / 4000 [ 20%] (Warmup)
## Chain 3, Iteration: 1200 / 4000 [ 30%] (Warmup)
## Chain 3, Iteration: 1600 / 4000 [ 40%] (Warmup)
## Chain 3, Iteration: 2000 / 4000 [ 50%] (Warmup)
## Chain 3, Iteration: 2001 / 4000 [ 50%] (Sampling)
## Chain 3, Iteration: 2400 / 4000 [ 60%] (Sampling)
## Chain 3, Iteration: 2800 / 4000 [ 70%] (Sampling)
## Chain 3, Iteration: 3200 / 4000 [ 80%] (Sampling)
## Chain 3, Iteration: 3600 / 4000 [ 90%] (Sampling)
## Chain 3, Iteration: 4000 / 4000 [100%] (Sampling)
## Elapsed Time: 0.326872 seconds (Warm-up)
## 0.288224 seconds (Sampling)
## 0.615096 seconds (Total)
##
##
## SAMPLING FOR MODEL 'surv ~ dbinom(density, p)' NOW (CHAIN 4).
##
## Chain 4, Iteration: 1 / 4000 [ 0%] (Warmup)
## Chain 4, Iteration: 400 / 4000 [ 10%] (Warmup)
## Chain 4, Iteration: 800 / 4000 [ 20%] (Warmup)
## Chain 4, Iteration: 1200 / 4000 [ 30%] (Warmup)
## Chain 4, Iteration: 1600 / 4000 [ 40%] (Warmup)
## Chain 4, Iteration: 2000 / 4000 [ 50%] (Warmup)
## Chain 4, Iteration: 2001 / 4000 [ 50%] (Sampling)
## Chain 4, Iteration: 2400 / 4000 [ 60%] (Sampling)
## Chain 4, Iteration: 2800 / 4000 [ 70%] (Sampling)
## Chain 4, Iteration: 3200 / 4000 [ 80%] (Sampling)
## Chain 4, Iteration: 3600 / 4000 [ 90%] (Sampling)
## Chain 4, Iteration: 4000 / 4000 [100%] (Sampling)
## Elapsed Time: 0.344322 seconds (Warm-up)
## 0.299568 seconds (Sampling)
## 0.64389 seconds (Total)
## The following numerical problems occured the indicated number of times after warmup on chain 4
## count
## Exception thrown at line 17: normal_log: Scale parameter is 0, but must be > 0! 1
## When a numerical problem occurs, the Hamiltonian proposal gets rejected.
## See http://mc-stan.org/misc/warnings.html#exception-hamiltonian-proposal-rejected
## If the number in the 'count' column is small, do not ask about this message on stan-users.
## Warning in FUN(X[[i]], ...): data with name pred is not numeric and not
## used
## Warning in FUN(X[[i]], ...): data with name size is not numeric and not
## used
##
## SAMPLING FOR MODEL 'surv ~ dbinom(density, p)' NOW (CHAIN 1).
## WARNING: No variance estimation is
## performed for num_warmup < 20
##
##
## Chain 1, Iteration: 1 / 1 [100%] (Sampling)
## Elapsed Time: 7e-06 seconds (Warm-up)
## 0.000126 seconds (Sampling)
## 0.000133 seconds (Total)
## Computing WAIC
## Constructing posterior predictions
## [ 800 / 8000 ]
[ 1600 / 8000 ]
[ 2400 / 8000 ]
[ 3200 / 8000 ]
[ 4000 / 8000 ]
[ 4800 / 8000 ]
[ 5600 / 8000 ]
[ 6400 / 8000 ]
[ 7200 / 8000 ]
[ 8000 / 8000 ]
## Aggregated binomial counts detected. Splitting to 0/1 outcome for WAIC calculation.
plot(m12m1.tank,ask=FALSE)
## Waiting to draw page 2 of 4
## Waiting to draw page 3 of 4
## Waiting to draw page 4 of 4
precis(m12m1.tank)
## 48 vector or matrix parameters omitted in display. Use depth=2 to show them.
## Mean StdDev lower 0.89 upper 0.89 n_eff Rhat
## a 1.30 0.25 0.90 1.68 8000 1
## sigma 1.62 0.21 1.28 1.94 5000 1
with predation
d$pred2 <- ifelse(d$pred=="pred",1,0)
m12m1.tank.pred <- map2stan(
alist(
surv ~ dbinom( density , p ) ,
logit(p) <- a_tank[tank] + b_pred*pred2 ,
a_tank[tank] ~ dnorm( a , sigma ) ,
a ~ dnorm(0,1) ,
sigma ~ dcauchy(0,1),
b_pred ~ dnorm(0,5)
), data=d , iter=4000 , chains=4 )
## Warning in FUN(X[[i]], ...): data with name pred is not numeric and not
## used
## Warning in FUN(X[[i]], ...): data with name size is not numeric and not
## used
## The following numerical problems occured the indicated number of times after warmup on chain 3
## count
## Exception thrown at line 20: normal_log: Scale parameter is 0, but must be > 0! 1
## When a numerical problem occurs, the Hamiltonian proposal gets rejected.
## See http://mc-stan.org/misc/warnings.html#exception-hamiltonian-proposal-rejected
## If the number in the 'count' column is small, do not ask about this message on stan-users.
## Warning in FUN(X[[i]], ...): data with name pred is not numeric and not
## used
## Warning in FUN(X[[i]], ...): data with name size is not numeric and not
## used
## Computing WAIC
## Constructing posterior predictions
## Aggregated binomial counts detected. Splitting to 0/1 outcome for WAIC calculation.
plot(m12m1.tank.pred,ask=FALSE)
## Waiting to draw page 2 of 4
## Waiting to draw page 3 of 4
## Waiting to draw page 4 of 4
precis(m12m1.tank.pred)
## 48 vector or matrix parameters omitted in display. Use depth=2 to show them.
## Mean StdDev lower 0.89 upper 0.89 n_eff Rhat
## a 2.55 0.23 2.18 2.91 1219 1
## sigma 0.83 0.14 0.61 1.05 2977 1
## b_pred -2.52 0.29 -2.98 -2.04 965 1
with size
d$big <- ifelse(d$size=="big",1,0)
m12m1.tank.size <- map2stan(
alist(
surv ~ dbinom( density , p ) ,
logit(p) <- a_tank[tank] + b_big*big ,
a_tank[tank] ~ dnorm( a , sigma ) ,
a ~ dnorm(0,1) ,
sigma ~ dcauchy(0,1),
b_big ~ dnorm(0,5)
), data=d , iter=4000 , chains=4 )
## Warning in FUN(X[[i]], ...): data with name pred is not numeric and not
## used
## Warning in FUN(X[[i]], ...): data with name size is not numeric and not
## used
## The following numerical problems occured the indicated number of times after warmup on chain 1
## count
## Exception thrown at line 20: normal_log: Scale parameter is 0, but must be > 0! 1
## When a numerical problem occurs, the Hamiltonian proposal gets rejected.
## See http://mc-stan.org/misc/warnings.html#exception-hamiltonian-proposal-rejected
## If the number in the 'count' column is small, do not ask about this message on stan-users.
## The following numerical problems occured the indicated number of times after warmup on chain 4
## count
## Exception thrown at line 20: normal_log: Scale parameter is 0, but must be > 0! 2
## When a numerical problem occurs, the Hamiltonian proposal gets rejected.
## See http://mc-stan.org/misc/warnings.html#exception-hamiltonian-proposal-rejected
## If the number in the 'count' column is small, do not ask about this message on stan-users.
## Warning in FUN(X[[i]], ...): data with name pred is not numeric and not
## used
## Warning in FUN(X[[i]], ...): data with name size is not numeric and not
## used
## Computing WAIC
## Constructing posterior predictions
## Aggregated binomial counts detected. Splitting to 0/1 outcome for WAIC calculation.
plot(m12m1.tank.size,ask=FALSE)
## Waiting to draw page 2 of 4
## Waiting to draw page 3 of 4
## Waiting to draw page 4 of 4
precis(m12m1.tank.size)
## 48 vector or matrix parameters omitted in display. Use depth=2 to show them.
## Mean StdDev lower 0.89 upper 0.89 n_eff Rhat
## a 1.43 0.34 0.88 1.97 1473 1
## sigma 1.62 0.21 1.27 1.93 5338 1
## b_big -0.27 0.50 -1.11 0.49 858 1
additive, with pred and size
m12m1.tank.pred.size <- map2stan(
alist(
surv ~ dbinom( density , p ) ,
logit(p) <- a_tank[tank] + b_big*big + b_pred*pred2,
a_tank[tank] ~ dnorm( a , sigma ) ,
a ~ dnorm(0,1) ,
sigma ~ dcauchy(0,1),
c(b_big,b_pred) ~ dnorm(0,5)
), data=d , iter=4000 , chains=4 )
## Warning in FUN(X[[i]], ...): data with name pred is not numeric and not
## used
## Warning in FUN(X[[i]], ...): data with name size is not numeric and not
## used
## The following numerical problems occured the indicated number of times after warmup on chain 1
## count
## Exception thrown at line 23: normal_log: Scale parameter is 0, but must be > 0! 1
## When a numerical problem occurs, the Hamiltonian proposal gets rejected.
## See http://mc-stan.org/misc/warnings.html#exception-hamiltonian-proposal-rejected
## If the number in the 'count' column is small, do not ask about this message on stan-users.
## The following numerical problems occured the indicated number of times after warmup on chain 3
## count
## Exception thrown at line 23: normal_log: Scale parameter is 0, but must be > 0! 1
## When a numerical problem occurs, the Hamiltonian proposal gets rejected.
## See http://mc-stan.org/misc/warnings.html#exception-hamiltonian-proposal-rejected
## If the number in the 'count' column is small, do not ask about this message on stan-users.
## The following numerical problems occured the indicated number of times after warmup on chain 4
## count
## Exception thrown at line 23: normal_log: Scale parameter is 0, but must be > 0! 1
## When a numerical problem occurs, the Hamiltonian proposal gets rejected.
## See http://mc-stan.org/misc/warnings.html#exception-hamiltonian-proposal-rejected
## If the number in the 'count' column is small, do not ask about this message on stan-users.
## Warning in FUN(X[[i]], ...): data with name pred is not numeric and not
## used
## Warning in FUN(X[[i]], ...): data with name size is not numeric and not
## used
## Computing WAIC
## Constructing posterior predictions
## Aggregated binomial counts detected. Splitting to 0/1 outcome for WAIC calculation.
plot(m12m1.tank.pred.size,ask=FALSE)
## Waiting to draw page 2 of 4
## Waiting to draw page 3 of 4
## Waiting to draw page 4 of 4
precis(m12m1.tank.pred.size)
## 48 vector or matrix parameters omitted in display. Use depth=2 to show them.
## Mean StdDev lower 0.89 upper 0.89 n_eff Rhat
## a 2.73 0.26 2.30 3.14 853 1.01
## sigma 0.79 0.14 0.56 1.00 2504 1.00
## b_big -0.37 0.29 -0.84 0.09 1606 1.00
## b_pred -2.51 0.29 -2.98 -2.05 1263 1.00
interaction, with pred and size
m12m1.tank.pred.size.int <- map2stan(
alist(
surv ~ dbinom( density , p ) ,
logit(p) <- a_tank[tank] + b_big*big + b_pred*pred2 + b_big_pred*big*pred2,
a_tank[tank] ~ dnorm( a , sigma ) ,
a ~ dnorm(0,1) ,
sigma ~ dcauchy(0,1),
c(b_big,b_pred,b_big_pred) ~ dnorm(0,5)
), data=d , iter=4000 , chains=4 )
## Warning in FUN(X[[i]], ...): data with name pred is not numeric and not
## used
## Warning in FUN(X[[i]], ...): data with name size is not numeric and not
## used
## The following numerical problems occured the indicated number of times after warmup on chain 1
## count
## Exception thrown at line 25: normal_log: Scale parameter is 0, but must be > 0! 1
## When a numerical problem occurs, the Hamiltonian proposal gets rejected.
## See http://mc-stan.org/misc/warnings.html#exception-hamiltonian-proposal-rejected
## If the number in the 'count' column is small, do not ask about this message on stan-users.
## The following numerical problems occured the indicated number of times after warmup on chain 3
## count
## Exception thrown at line 25: normal_log: Scale parameter is 0, but must be > 0! 2
## When a numerical problem occurs, the Hamiltonian proposal gets rejected.
## See http://mc-stan.org/misc/warnings.html#exception-hamiltonian-proposal-rejected
## If the number in the 'count' column is small, do not ask about this message on stan-users.
## Warning in FUN(X[[i]], ...): data with name pred is not numeric and not
## used
## Warning in FUN(X[[i]], ...): data with name size is not numeric and not
## used
## Computing WAIC
## Constructing posterior predictions
## Aggregated binomial counts detected. Splitting to 0/1 outcome for WAIC calculation.
plot(m12m1.tank.pred.size.int,ask=FALSE)
## Waiting to draw page 2 of 4
## Waiting to draw page 3 of 4
## Waiting to draw page 4 of 4
precis(m12m1.tank.pred.size.int)
## 48 vector or matrix parameters omitted in display. Use depth=2 to show them.
## Mean StdDev lower 0.89 upper 0.89 n_eff Rhat
## a 2.37 0.30 1.91 2.87 677 1.00
## sigma 0.75 0.15 0.50 0.96 1150 1.01
## b_big 0.41 0.46 -0.32 1.13 1014 1.00
## b_pred -1.89 0.40 -2.52 -1.26 790 1.00
## b_big_pred -1.34 0.59 -2.25 -0.40 1322 1.00
par(mfrow=c(1,1))
Focus on the inferred variation across tanks. Explain why it changes as it does across models
At first pass we can just look at the sigma parameter from each model as this is the estimate of adaptive estimate of standard deviation from tank to tank.
precis(m12m1.tank)
## 48 vector or matrix parameters omitted in display. Use depth=2 to show them.
## Mean StdDev lower 0.89 upper 0.89 n_eff Rhat
## a 1.30 0.25 0.90 1.68 8000 1
## sigma 1.62 0.21 1.28 1.94 5000 1
precis(m12m1.tank.pred)
## 48 vector or matrix parameters omitted in display. Use depth=2 to show them.
## Mean StdDev lower 0.89 upper 0.89 n_eff Rhat
## a 2.55 0.23 2.18 2.91 1219 1
## sigma 0.83 0.14 0.61 1.05 2977 1
## b_pred -2.52 0.29 -2.98 -2.04 965 1
precis(m12m1.tank.size)
## 48 vector or matrix parameters omitted in display. Use depth=2 to show them.
## Mean StdDev lower 0.89 upper 0.89 n_eff Rhat
## a 1.43 0.34 0.88 1.97 1473 1
## sigma 1.62 0.21 1.27 1.93 5338 1
## b_big -0.27 0.50 -1.11 0.49 858 1
precis(m12m1.tank.pred.size)
## 48 vector or matrix parameters omitted in display. Use depth=2 to show them.
## Mean StdDev lower 0.89 upper 0.89 n_eff Rhat
## a 2.73 0.26 2.30 3.14 853 1.01
## sigma 0.79 0.14 0.56 1.00 2504 1.00
## b_big -0.37 0.29 -0.84 0.09 1606 1.00
## b_pred -2.51 0.29 -2.98 -2.05 1263 1.00
precis(m12m1.tank.pred.size.int)
## 48 vector or matrix parameters omitted in display. Use depth=2 to show them.
## Mean StdDev lower 0.89 upper 0.89 n_eff Rhat
## a 2.37 0.30 1.91 2.87 677 1.00
## sigma 0.75 0.15 0.50 0.96 1150 1.01
## b_big 0.41 0.46 -0.32 1.13 1014 1.00
## b_pred -1.89 0.40 -2.52 -1.26 790 1.00
## b_big_pred -1.34 0.59 -2.25 -0.40 1322 1.00
Basically we see that having predation in the model reduces variance among tanks. This is because predation is a strong predicor of survival, so including it in the model reduces the otherwise unexplained tank to tank variance.
Compare the models you fit just above, using WAIC. Can you reconcile the differences in WAIC with the posterior distributions of the models?
compare(m12m1.tank,m12m1.tank.pred,m12m1.tank.size,m12m1.tank.pred.size,m12m1.tank.pred.size.int)
## WAIC pWAIC dWAIC weight SE dSE
## m12m1.tank.pred 999.9 28.6 0.0 0.46 37.34 NA
## m12m1.tank.pred.size 1000.8 28.2 0.9 0.29 37.40 1.58
## m12m1.tank.pred.size.int 1001.1 28.1 1.2 0.25 37.71 2.92
## m12m1.tank 1009.5 37.7 9.6 0.00 37.98 6.47
## m12m1.tank.size 1010.4 38.2 10.5 0.00 38.10 6.54
Models that include pred have a smaller number of effective parameters and a lower WAIC. This makes sense w.r.t. the posterior distributions; tanks
m12m1.tank.pred.size.int.b <-
brm(surv | trials(density) ~ 0 + (1| tank) + pred*size,
data=d,
family=binomial(link = "logit"),
prior=c(set_prior("cauchy(0,1)", class = "sd"),
set_prior("normal(0,5)", class = "b")))
## Compiling the C++ model
plot(m12m1.tank.pred.size.int.b)
m12m1.tank.pred.size.int.b
## Family: binomial (logit)
## Formula: surv | trials(density) ~ 0 + (1 | tank) + pred * size
## Data: d (Number of observations: 48)
## Samples: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
## total post-warmup samples = 4000
## WAIC: Not computed
##
## Group-Level Effects:
## ~tank (Number of levels: 48)
## Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
## sd(Intercept) 0.74 0.14 0.49 1.06 1340 1
##
## Population-Level Effects:
## Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
## predno 2.76 0.33 2.13 3.44 2333 1
## predpred -0.44 0.25 -0.92 0.07 1999 1
## sizesmall -0.15 0.44 -1.05 0.70 2287 1
## predpred:sizesmall 1.07 0.56 -0.02 2.15 1930 1
##
## Samples were drawn using sampling(NUTS). For each parameter, Eff.Sample
## is a crude measure of effective sample size, and Rhat is the potential
## scale reduction factor on split chains (at convergence, Rhat = 1).
precis(m12m1.tank.pred.size.int)
## 48 vector or matrix parameters omitted in display. Use depth=2 to show them.
## Mean StdDev lower 0.89 upper 0.89 n_eff Rhat
## a 2.37 0.30 1.91 2.87 677 1.00
## sigma 0.75 0.15 0.50 0.96 1150 1.01
## b_big 0.41 0.46 -0.32 1.13 1014 1.00
## b_pred -1.89 0.40 -2.52 -1.26 790 1.00
## b_big_pred -1.34 0.59 -2.25 -0.40 1322 1.00